<?php

error_reporting(E_ALL);
ob_start();
include_once "../connect.php";
// รับค่ามาเก็บไว้ใน 
$cn_id = $_POST["cn_id"];
$cn_number = $_POST["cn_number"];
$cn_comeName = $_POST["cn_comeName"];
//$cn_department = $_POST["cn_department"];
$cn_ip = $_POST["cn_ip"];
$id_member = $_POST["id_member"];
//$cn_createName = $_POST["cn_createName"];
$c_mb = $_POST["c_mb"];
$c_vga = $_POST["c_vga"];
$c_cpu = $_POST["c_cpu"];
$c_fdd = $_POST["c_fdd"];
$c_ram = $_POST["c_ram"];
$c_keyboard = $_POST["c_keyboard"];
$c_hdd = $_POST["c_hdd"];
$c_mouse = $_POST["c_mouse"];
$c_sound = $_POST["c_sound"];
$c_monitor = $_POST["c_monitor"];
$c_cdRom = $_POST["c_cdRom"];
$c_speaker = $_POST["c_speaker"];
$c_lan = $_POST["c_lan"];
$c_notic = $_POST["c_notic"];
$oth_name = $_POST["oth_name"];
$oth_detail = $_POST["oth_detail"];
$emp_id = $_POST["emp_id"];
//$c_nameUser = $_POST["c_nameUser"];
$hid_department = $_POST["hid_department"];
$oth_name2 = $_POST["oth_name2"];
$oth_detail2 = $_POST["oth_detail2"];
$oth_name3 = $_POST["oth_name3"];
$oth_detail3 = $_POST["oth_detail3"];


$sqlCnNumber = " select * from comname where cn_number = '$cn_number' and cn_id != '$cn_id' ";
$CN_Number = mysql_query($sqlCnNumber);
if (mysql_num_rows($CN_Number) != 0) {
    echo "	<script language='javascript'>
		alert('ชื่อเครื่องซ้ำครับ');
		setTimeout(\"parent.$.fancybox.close()\",1000);
		</script>";
    exit();
}



if (empty($cn_id)) {
    $newNum = mysql_result(mysql_query(" SELECT Max( c_refNumber )+1 AS MaxID FROM computer "), 0, "MaxID");

    if ($newNum == NULL) { // ถ้าได้เป็นค่าว่าง หรือ null ก็แสดงว่ายังไม่มีข้อมูลในฐานข้อมูล
        $newNum = "1";
    } else {
        mysql_query($newNum); //ถ้าไม่ใช่ค่าว่าง
    }


    $sql1 = "
INSERT INTO comname (
`cn_number` ,
`cn_comeName` ,
`de_id` ,
`cn_ip` ,
`id_member` ,
`emp_id` ,
`cn_createdate` 
)
VALUES (
'$cn_number', 
    '$cn_comeName',
    '$hid_department', 
        '$cn_ip', 
      '$id_member',         
     '$emp_id',
           NOW() 
)";

    mysql_query($sql1);
    $Updatecnid = mysql_fetch_array(mysql_query('SELECT MAX(cn_id) FROM comname'));
    $CN_ID = $Updatecnid[0];

    $sql2 = "  
        INSERT INTO `db_itmanager`.`computer` (
`c_mb` ,
`c_cpu` ,
`c_ram` ,
`c_hdd` ,
`c_sound` ,
`c_cdRom` ,
`c_lan` ,
`c_vga` ,
`c_fdd` ,
`c_keyboard` ,
`c_mouse` ,
`c_monitor` ,
`c_speaker` ,
`c_notic` ,
`c_refNumber` ,
`cn_id` ,
`emp_id` ,
`cn_number`
)
VALUES (
 '$c_mb', '$c_cpu','$c_ram', '$c_hdd', '$c_sound', '$c_cdRom', '$c_lan', '$c_vga', '$c_fdd', '$c_keyboard', '$c_mouse','$c_monitor', '$c_speaker', '$c_notic', '$newNum', '$CN_ID', '$emp_id',  '$cn_number'
)";

   
 

} else {

    header("location:listCom.php");
}


if (mysql_query($sql2)) {

        //  header("location:listCom.php");


        $sqlUpdatecnid = "UPDATE employee SET cn_id='$CN_ID' WHERE emp_id = '$emp_id'";

        mysql_query($sqlUpdatecnid);

        $upKey = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_keyboard'";
        mysql_query($upKey);

        $upMou = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_mouse'";
        mysql_query($upMou);

        $upMoni = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_monitor'";
        mysql_query($upMoni);

        $upCom = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$cn_number'";
        mysql_query($upCom);

        
        $exeCom = "SELECT * FROM product_detail WHERE pd_id = '$cn_number'";
        $rs = mysql_query($exeCom);

        if ($rs) {

            $r = mysql_fetch_array($rs);
            $Prd_id = $r['prd_id'];

            $exeComDe = "SELECT * FROM product WHERE prd_id = '$Prd_id'";
            $rsComD = mysql_query($exeComDe);

            $rComDe = mysql_fetch_array($rsComD);
            $Prd_balance = $rComDe['prd_balance'];

            $upComDe = "UPDATE product SET prd_balance = '$Prd_balance' - '1' WHERE prd_id = '$Prd_id'  ";

            mysql_query($upComDe);
        } else {
            echo mysql_error();
        }
        $exeMoniDe = "SELECT * FROM product_detail WHERE pd_code = '$c_monitor'";
        $rsMoniDe = mysql_query($exeMoniDe);

        if ($rsMoniDe) {
            $rMoniDe = mysql_fetch_array($rsMoniDe);
            $Prd_idMoniDe = $rMoniDe['prd_id'];

            $exeMoni = "SELECT * FROM product WHERE prd_id = '$Prd_idMoniDe'";
            $rsMoni = mysql_query($exeMoni);
            $rMoni = mysql_fetch_array($rsMoni);
            $Prd_balanceMoni = $rMoni['prd_balance'];

            $upMoniBalan = "UPDATE product SET prd_balance = '$Prd_balanceMoni' - '1' WHERE prd_id = '$Prd_idMoniDe'";

            mysql_query($upMoniBalan);
        } else {
            echo mysql_error();
        }

        $exeMousDe = "SELECT * FROM product_detail WHERE pd_id = '$c_mouse'";
        $rsMouseDe = mysql_query($exeMousDe);
        if ($rsMouseDe) {
            $rMousDe = mysql_fetch_array($rsMouseDe);
            $prd_idMouse = $rMousDe['prd_id'];

            $exeMouse = "SELECT * FROM product WHERE prd_id = '$prd_idMouse' ";
            $rsMouse = mysql_query($exeMouse);
            $rMouse = mysql_fetch_array($rsMouse);
            $Prd_balanceMou = $rMouse['prd_balance'];

            $upMouseBal = "UPDATE  product SET prd_balance = '$Prd_balanceMou' - '1' WHERE prd_id = '$prd_idMouse'";
            mysql_query($upMouseBal);
        } else {
            echo mysql_error();
        }

        $exeKeyDe = "SELECT * FROM product_detail WHERE pd_id = '$c_keyboard'";
        $rsKeyDe = mysql_query($exeKeyDe);
        if ($rsKeyDe) {
            $rKeyDe = mysql_fetch_array($rsKeyDe);
            $Prd_idKey = $rKeyDe['prd_id'];

            $exeKey = "SELECT * FROM product WHERE prd_id = '$Prd_idKey'";
            $rsKey = mysql_query($exeKey);
            $rKey = mysql_fetch_array($rsKey);
            $Prd_balKey = $rKey['prd_balance'];

            $upKey = "UPDATE product SET prd_balance = '$Prd_balKey' - '1' WHERE prd_id = '$Prd_idKey' ";
            mysql_query($upKey);
        } else {
            echo mysql_error();
        }

        $exeOthde = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail'";
        $rsOthde = mysql_query($exeOthde);
        if ($rsOthde) {
            $rOthde = mysql_fetch_array($rsOthde);
            $Prd_idOthde = $rOthde['prd_id'];

            $exeOth = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde'";
            $rsOth = mysql_query($exeOth);
            $rOth = mysql_fetch_array($rsOth);
            $Prd_balOth = $rOth['prd_balance'];

            $upOth = "UPDATE product SET prd_balance = '$Prd_balOth' - '1' WHERE prd_id = '$Prd_idOthde' ";
            mysql_query($upOth);
        } else {
            echo mysql_error();
        }


        echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
        echo "<meta http-equiv='refresh' content='5; url=listCom.php'>";
    
} else {
    echo mysql_error();
}

    
if ($oth_detail != NULL) {
    $sqlOther = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name', '$oth_detail', '$cn_number' , '$CN_ID', '$emp_id'
)";
    mysql_query($sqlOther);

    $upOth = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail'";
    mysql_query($upOth);

    $exeOthde = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail2'";
    $rsOthde = mysql_query($exeOthde);
    if ($rsOthde) {
        $rOthde = mysql_fetch_array($rsOthde);
        $Prd_idOthde = $rOthde['prd_id'];

        $exeOth = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde'";
        $rsOth = mysql_query($exeOth);
        $rOth = mysql_fetch_array($rsOth);
        $Prd_balOth = $rOth['prd_balance'];

        $upOth = "UPDATE product SET prd_balance = '$Prd_balOth' - '1' WHERE prd_id = '$Prd_idOthde' ";
        mysql_query($upOth);
    } else {
        echo mysql_error();
    }
}
  
    
if ($oth_detail2 != NULL) {
    $sqlOther2 = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name2', '$oth_detail2', '$cn_number' , '$CN_ID', '$emp_id'
)";
    mysql_query($sqlOther2);

    $upOth2 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail2'";
    mysql_query($upOth2);

    $exeOthde2 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail2'";
    $rsOthde2 = mysql_query($exeOthde2);
    if ($rsOthde2) {
        $rOthde2 = mysql_fetch_array($rsOthde2);
        $Prd_idOthde2 = $rOthde2['prd_id'];

        $exeOth2 = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde2'";
        $rsOth2 = mysql_query($exeOth2);
        $rOth2 = mysql_fetch_array($rsOth2);
        $Prd_balOth2 = $rOth2['prd_balance'];

        $upOth2 = "UPDATE product SET prd_balance = '$Prd_balOth2' - '1' WHERE prd_id = '$Prd_idOthde2' ";
        mysql_query($upOth2);
    } else {
        echo mysql_error();
    }
}

if ($oth_detail3 != NULL) {
    $sqlOther3 = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name3', '$oth_detail3', '$cn_number' , '$CN_ID', '$emp_id'
)";
    mysql_query($sqlOther3);

    $upOth3 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail3'";
    mysql_query($upOth3);

    $exeOthde3 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail3'";
    $rsOthde3 = mysql_query($exeOthde3);
    if ($rsOthde3) {
        $rOthde3 = mysql_fetch_array($rsOthde3);
        $Prd_idOthde3 = $rOthde3['prd_id'];

        $exeOth3 = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde3'";
        $rsOth3 = mysql_query($exeOth3);
        $rOth3 = mysql_fetch_array($rsOth3);
        $Prd_balOth3 = $rOth3['prd_balance'];

        $upOth3 = "UPDATE product SET prd_balance = '$Prd_balOth3' - '1' WHERE prd_id = '$Prd_idOthde3' ";
        mysql_query($upOth3);
    } else {
        echo mysql_error();
    }
}

?>
